package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.Cart;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.CartCheckoutVo;
import com.cskaoyan.bean.vo.wx.CartIndexVo;
import com.cskaoyan.exception.CartException;
import com.cskaoyan.service.CartService;
import com.cskaoyan.utils.InvalidatedParamsUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author yfsper233
 * @time 2021/7/10-15:53
 */
@RestController
@RequestMapping("wx/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("index")
    public BaseRespVo cartIndex() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        CartIndexVo cartIndexVo = cartService.queryCartIndex(user.getId(), 0, false);

        return BaseRespVo.ok(cartIndexVo);
    }

    @PostMapping("checked")
    public BaseRespVo cartChecked(@RequestBody CartCheckedBo checkedBo) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        Integer result = cartService.changeCartCheckedStatus(user.getId(), checkedBo);
        if (result == checkedBo.getProductIds().size()) {
            CartIndexVo cartIndexVo = cartService.queryCartIndex(user.getId(), 0, false);
            return BaseRespVo.ok(cartIndexVo);
        }
        return BaseRespVo.fail("服务器正在升级维护，请稍后再试");
    }

    @GetMapping("checkout")
    @Transactional(rollbackFor = Exception.class)
    public BaseRespVo cartCheckout(CartCheckoutBo checkoutBo) throws CartException {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }

        CartIndexVo cartCheckedList = cartService.queryCartIndex(user.getId(), checkoutBo.getCartId(), true);

        if (checkoutBo.getCartId()>0){//快速下单的情况，不应在cart中维护其记录，所以要删除，不能让其出现在购物车中
            cartService.removeCartByCartId(checkoutBo.getCartId());
        }
        CartCheckoutVo cartCheckoutVo = cartService.queryCartCheckoutInfo(user.getId(), checkoutBo, cartCheckedList);

        return BaseRespVo.ok(cartCheckoutVo);
    }

    @GetMapping("goodscount")
    public BaseRespVo cartGoodsCount() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        Integer result = cartService.queryCartGoodsCount(user.getId());
        return BaseRespVo.ok(result);
    }

    @PostMapping("add")
    public BaseRespVo cartAdd(@RequestBody @Validated CartAddBo cartAddBo, BindingResult bindingResult) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }

        if (bindingResult.hasFieldErrors()) {
            InvalidatedParamsUtil.resErrorVo(bindingResult);
        }

        cartService.addCart(cartAddBo, user.getId(),false);

        Integer count = cartService.queryCartGoodsCount(user.getId());

        return BaseRespVo.ok(count);   //返回购物车中现有的商品数量

    }

    @PostMapping("fastadd")
    @Transactional(rollbackFor = Exception.class)
    public BaseRespVo cartFastAdd(@RequestBody @Validated CartAddBo cartAddBo, BindingResult bindingResult) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        if (bindingResult.hasFieldErrors()) {
            InvalidatedParamsUtil.resErrorVo(bindingResult);
        }
        Integer result = cartService.addCart(cartAddBo, user.getId(),true);

        return BaseRespVo.ok(result);//返回的是新增cartId
    }

    @PostMapping("update")
    public BaseRespVo cartUpdate(@RequestBody CartUpdateBo cartUpdateBo) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        Integer result = cartService.changeCartProductNum(cartUpdateBo);
        if (result == 200) {
            return BaseRespVo.ok("更新成功");
        }
        if (result==400){
            return BaseRespVo.fail("库存不足，添加失败");
        }
       return BaseRespVo.fail();
    }

    @PostMapping("delete")
    public BaseRespVo cartDelete(@RequestBody CartDeleteBo cartDeleteBo) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        Integer result = cartService.removeCartByUserIdAndProductIds(cartDeleteBo.getProductIds(), 1);
        if (result == cartDeleteBo.getProductIds().size()) {
            CartIndexVo cartIndex = cartService.queryCartIndex(user.getId(), 0, false);
            return BaseRespVo.ok(cartIndex);
        }
        return BaseRespVo.fail("该条记录不存在，或已经被删除");
    }


}
